package com.huawei.hwphy2d.physical.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import com.huawei.hwphy2d.physical.config.json.Config;
import com.huawei.hwphy2d.physical.util.AxisTranslater;
import com.huawei.hwphy2d.physical.util.DeviceParameters;
import com.huawei.hwphy2d.physical.util.RandomRange;
import o.fyz;
import o.fzf;
import o.fzg;
import o.fzp;
import o.fzs;
import o.fzt;
import o.fzw;
import o.fzy;
import o.gac;

public class HwBody {
    private fzw bodyDef;
    private Config config;
    private fzy fixtureDef;
    Bitmap mBitmap;
    fzt mBody;
    private Context mContext;
    private float mHeight;
    Matrix mMatrix;
    Paint mPaint;
    private float mWidth;
    private gac mWorld;
    private float sizeRate = 0.0f;

    public HwBody(gac o_gac, float f, Context context) {
        this.mWorld = o_gac;
        this.sizeRate = f;
        this.mContext = context;
    }

    public void build(Bitmap bitmap, Config config) {
        this.mBitmap = bitmap;
        this.config = config;
        init();
        initShapeDef();
        createDynamicActor();
    }

    private void init() {
        this.mMatrix = new Matrix();
        this.mPaint = new Paint(1);
        if (this.mBitmap != null) {
            this.mWidth = ((float) this.mBitmap.getWidth()) * this.sizeRate;
            this.mHeight = ((float) this.mBitmap.getHeight()) * this.sizeRate;
        }
    }

    public void draw(Canvas canvas) {
        fzp ˎ = this.mBody.ˎ();
        this.mMatrix.reset();
        float transBodyToPosition = AxisTranslater.transBodyToPosition(ˎ.ˊ);
        float transBodyToPosition2 = AxisTranslater.transBodyToPosition(ˎ.ॱ);
        float abs = Math.abs(((float) ((((double) this.mBody.ˋ()) / 3.141592653589793d) * 180.0d)) % 360.0f);
        if (this.mWidth > 20.0f && this.mHeight > 20.0f) {
            this.mMatrix.postScale(this.sizeRate, this.sizeRate, this.mWidth / 2.0f, this.mHeight / 2.0f);
        }
        if (this.config.getStage().getGravity_field().getY() == 0) {
            this.mMatrix.preRotate(180.0f, this.mWidth / 2.0f, this.mHeight / 2.0f);
        } else {
            this.mMatrix.preRotate(abs, this.mWidth / 2.0f, this.mHeight / 2.0f);
        }
        this.mMatrix.postTranslate(transBodyToPosition - ((float) Math.floor((double) (this.mWidth / 2.0f))), transBodyToPosition2 - ((float) Math.floor((double) (this.mHeight / 2.0f))));
        if (this.mBitmap != null) {
            canvas.drawBitmap(this.mBitmap, this.mMatrix, this.mPaint);
        }
        if (this.config.getStage().isDebug_draw()) {
            drawDebug(canvas);
        }
    }

    @Deprecated
    public void setTargetAngle(float f, fzt o_fzt) {
        float ˋ = f - (o_fzt.ˋ() + (o_fzt.ॱ() * 0.0016666667f));
        while (ˋ < -180.0f * 0.05f) {
            ˋ += 360.0f * 0.05f;
        }
        while (ˋ > 180.0f * 0.05f) {
            ˋ -= 360.0f * 0.05f;
        }
        o_fzt.ॱ((ˋ / 0.0016666667f) * o_fzt.ʻ());
    }

    public void release() {
        if (this.mBody != null && this.mWorld != null) {
            this.mWorld.ॱ(this.mBody);
        }
    }

    public void setActive(boolean z) {
        this.mBody.ॱ(z);
    }

    public boolean isActive() {
        if (this.mBody != null) {
            return this.mBody.ˊॱ();
        }
        return false;
    }

    public void initShapeDef() {
        fzg o_fzg = null;
        if (this.config.getActor().getShape().equals("circle")) {
            o_fzg = new fyz();
            o_fzg.ˏ(Math.min(this.mWidth, this.mHeight) / 2.0f);
        } else if (this.config.getActor().getShape().equals("polygon")) {
            o_fzg = new fzf();
            if (this.mBitmap != null) {
                ((fzf) o_fzg).ˏ(AxisTranslater.transPositionToBody((this.mWidth / 4.0f) * this.sizeRate), AxisTranslater.transPositionToBody((this.mHeight / 4.0f) * this.sizeRate));
            }
        }
        this.bodyDef = new fzw();
        this.bodyDef.ॱ = fzs.ˊ;
        int nextRandomInt = RandomRange.getNextRandomInt(1, 360);
        int screenWidth = DeviceParameters.getScreenWidth(this.mContext);
        if (this.mWorld.ˏ().ॱ == 0.0f) {
            this.bodyDef.ˎ.ˎ(AxisTranslater.transPositionToBody((float) RandomRange.getNextRandomInt(0, screenWidth)), -50.0f);
        } else {
            if (this.config.getActor().isAuto_angle()) {
                this.bodyDef.ˎ((float) nextRandomInt);
            }
            this.bodyDef.ˎ.ˎ(AxisTranslater.transPositionToBody((float) RandomRange.getNextRandomInt(50, screenWidth - 50)), -AxisTranslater.transPositionToBody((float) RandomRange.getNextRandomInt((int) Math.ceil((double) this.mHeight), (int) Math.ceil((double) (this.mHeight * 2.0f)))));
        }
        this.fixtureDef = new fzy();
        this.fixtureDef.ˊ(o_fzg);
        this.fixtureDef.ˊ = ((float) this.config.getActor().getDensity()) * this.sizeRate;
        this.fixtureDef.ˏ = (float) this.config.getActor().getFriction();
        this.fixtureDef.ॱ = ((float) this.config.getActor().getRestitution()) * this.sizeRate;
    }

    public fzt createDynamicActor() {
        if (!(this.bodyDef == null || this.fixtureDef == null)) {
            if (this.mBody == null) {
                this.mBody = this.mWorld.ˊ(this.bodyDef);
                this.mBody.ॱ(false);
            }
            if (this.mBody != null) {
                this.mBody.ˋ(this.fixtureDef);
                fzp ˊ = this.mBody.ˊ();
                if (this.config.getActor().isLinear()) {
                    this.mBody.ˏ(new fzp(0.0f, (float) RandomRange.getNextRandomInt(20, 25)));
                } else {
                    this.mBody.ˏ(new fzp(0.0f, ˊ.ॱ() * 6.0f));
                }
                this.mBody.ॱॱ();
            }
        }
        return this.mBody;
    }

    public void setBody(fzt o_fzt) {
        this.mBody = o_fzt;
    }

    public fzt getBody() {
        return this.mBody;
    }

    public void drawDebug(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(4.0f);
        paint.setColor(-16711936);
        fzp ˎ = this.mBody.ˎ();
        float transBodyToPosition = AxisTranslater.transBodyToPosition(ˎ.ˊ) - (this.mWidth / 2.0f);
        float transBodyToPosition2 = AxisTranslater.transBodyToPosition(ˎ.ॱ) - (this.mHeight / 2.0f);
        float f = this.mWidth;
        float f2 = this.mHeight;
        float ˋ = ((float) ((((double) this.mBody.ˋ()) / 3.141592653589793d) * 180.0d)) % 360.0f;
        RectF rectF = new RectF(transBodyToPosition, transBodyToPosition2, transBodyToPosition + f, transBodyToPosition2 + f2);
        canvas.save();
        canvas.rotate(ˋ, transBodyToPosition + (f / 2.0f), transBodyToPosition2 + (f2 / 2.0f));
        canvas.drawRect(rectF, paint);
        canvas.restore();
    }

    public boolean inViewPoint(Rect rect) {
        if (AxisTranslater.transBodyToPosition(this.mBody.ˎ().ॱ) < ((float) rect.bottom)) {
            return true;
        }
        return false;
    }

    public float getHeight() {
        return this.mHeight;
    }
}
